package Semantics;

import SyntaxTree.XYZ2parserTreeConstants;

public class Table {
	public static int SUCCESS = 0;
	public static int DUPLICATE = -1;
	public static int UNEXPECTED = -2;
	
	private HashT varHash = new HashT();
	private HashT methodHash = new HashT();
	private HashT classHash = new HashT();
	
	public Table(){
		
	}
	
	public boolean compareType(String ta, String tb){	//true
		if(ta.equals(tb))
			return true;
		
		ClassSymbol csa = (ClassSymbol)classHash.lookup(ta);
		ClassSymbol csb = (ClassSymbol)classHash.lookup(tb);
		if((csa == null && csb != null) || (csa != null && csb == null))
			return false;
		
		else if(csa == null && csb == null){	// keyworld
			if((ta.equals("int") && tb.equals("long")) ||
					(ta.equals("long") && tb.equals("int")))
				return true;
		}
		else // 
		{
			if(csa.getSuper().equals(tb) || csb.getSuper().equals(ta))
				return true;
		}
		return false;
	}
	
	public int addSymbol(VarSymbol symbol){
		String id = symbol.getId();
		int it = symbol.getJJTType();
		
		if(it == XYZ2parserTreeConstants.JJTVARDECL){
			varHash.insert(id, symbol);
			return SUCCESS;
		}
		else if(it == XYZ2parserTreeConstants.JJTMETHODDECL){
			methodHash.insert(id, symbol);
		}
		else if(it == XYZ2parserTreeConstants.JJTCLASSDECL){
			ClassSymbol cs = (ClassSymbol)classHash.lookup(id);
			if(cs != null)
				return DUPLICATE;
			classHash.insert(id, (ClassSymbol)symbol);
			return SUCCESS;
		}
		return UNEXPECTED;
	}
	
	public int deleteVarSymbol(String id){
		varHash.pop(id);
		return 0;
	}
	
	public VarSymbol getVarSymbol(String id){
		VarSymbol s = (VarSymbol)varHash.lookup(id);
		return s;
	}
	
	public MethodSymbol getMethodSymbol(String id){	//TODO 	
		MethodSymbol s = (MethodSymbol)methodHash.lookup(id);
		return s;
	}

	public ClassSymbol getClassSymbol(String id){
		if(id == null || id.length() == 0)
			return null;
		
		ClassSymbol s = (ClassSymbol)classHash.lookup(id);
		return s;
	}
	
	public HashT getVarHash(){
		return varHash;
	}
	
	public HashT getMethodHash(){
		return methodHash;
	}
	
	public HashT getClassHash(){
		return classHash;
	}
	
	public void clearVarHash(){
		//varHash.clear();
		varHash = new HashT();
	}
	
	public void clearMethodHash(){
		//methodHash.clear();
		methodHash = new HashT();
	}
	
	public void printTable(){
		
	}
	
	public Object[] getAllClass(){
		return classHash.getall();
	}
	
	public void addTable(Table table){
		
	}
	
	public void setVarHash(HashT hash){
		varHash = hash;
	}
	
	public void setMethodHash(HashT hash){
		methodHash = hash;
	}
	
	public Object[] getAllMethods(String id){
		return methodHash.lookupall(id);
	}
}
